import type { Directive, DirectiveBinding } from 'vue';
import { checkPermi } from '@/utils/ruoyi';
import useUserStore from '@/store/user';
/**
 * 操作权限处理
 */
export const hasPermi: Directive = {
    mounted(el: HTMLElement, binding: DirectiveBinding) {
        // 「其他角色」按钮权限校验
        const { value } = binding;
        if (!checkPermi(value)) {
            el.parentNode && el.parentNode?.removeChild(el);
            return false;
        }
    }
};

/**
 * 角色权限处理
 */
export const hasRoles: Directive = {
    mounted(el: HTMLElement, binding: DirectiveBinding) {
        const { value } = binding;
        const { roles } = useUserStore();
        if (value && value instanceof Array && value.length > 0) {
            const hasRole = roles.some((role: string) => {
                return role === 'superadmin' || role === 'admin' || value.includes(role);
            });
            if (!hasRole) {
                el.parentNode && el.parentNode.removeChild(el);
                return false;
            }
        } else {
            throw new Error("check roles! Like v-has-roles=\"['admin','test']\"");
        }
    }
};
